package example_of_use;

import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.classifiers.bayes.NaiveBayesUpdateable;

import java.io.File;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;

/**
 * This example trains NaiveBayes incrementally on data obtained
 * from the ArffLoader.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
public class IncrementalClassifier {

  /**
   * Expects an ARFF file as first argument (class attribute is assumed
   * to be the last attribute).
   *
   * @param args        the commandline arguments
   * @throws Exception  if something goes wrong
   */
  public static void main(String[] args) throws Exception {
    args = new String[1];
    args[0] = "src/java/data_set/kuisib.arff";
    // load data
    ArffLoader loader = new ArffLoader();
    loader.setFile(new File(args[0]));
    Instances structure = loader.getStructure();
    structure.setClassIndex(structure.numAttributes() - 1);
    
    ArffLoader trueloader = new ArffLoader();
    trueloader.setFile(new File(args[0]));
    Instances truestructure = trueloader.getDataSet();
    truestructure.setClassIndex(truestructure.numAttributes()-1);
    System.out.println("True Structure \n" + truestructure);
    
    

    ArffLoader trueloader1 = new ArffLoader();
    trueloader1.setFile(new File("src/java/data_set/kuisibdouble.arff"));
    Instances truestructure1 = trueloader1.getDataSet();
    truestructure1.setClassIndex(truestructure.numAttributes()-1);
    
    // train NaiveBayes
//    NaiveBayesUpdateable nb = new NaiveBayesUpdateable();
    NaiveBayes nb = new NaiveBayes();
    
    nb.buildClassifier(truestructure);
    System.out.println("CIEE STRUCTURE " + structure);
    Instance current = loader.getNextInstance(structure);
    Instance temp = new Instance(current);
    while ((current = loader.getNextInstance(structure)) != null){
      System.out.println("EAA : " + current);
      System.out.println(current.stringValue(current.classIndex()));
      nb.updateClassifier(current);
      temp = new Instance(current);
      temp = current;
      System.out.println("EAATEMP : " + temp);
    }
    
    temp.setValue(temp.classIndex(), "T");
    System.out.println("EAATEMP : " + temp);
    System.out.println("Nilai final : "+ nb.classifyInstance(temp));
//    nb.
      
//     output generated model
//    System.out.println(nb);
//    Evaluation eval = new Evaluation(truestructure);
//    eval.evaluateModel(nb, truestructure);
////    eval.evaluateModel(nb, truestructure);
//    System.out.println(eval.toSummaryString());
  }
}
